/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.progressbar;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQProgressBarUtils
{

	/**
	 * Create a JQProgressBarBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQProgressBarBuilder
	 */
	public static JQProgressBarBuilder create(String id, String key)
	{
		return new JQProgressBarBuilder(new JQProgressBar(id, (key == null ? JQProgressBarPlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQProgressBarBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQProgressBarBuilder
	 */
	public static JQProgressBarBuilder create(String id)
	{
		return new JQProgressBarBuilder(new JQProgressBar(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQProgressBarBuilder
	{
		/** JQuery component */
		private JQProgressBar component;
		/** JQuery default options */
		private JQProgressBarOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQProgressBarBuilder(JQProgressBar component)
		{
			this.component = component;
			this.options = new JQProgressBarOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the duration option value
		 * 
		 * @param duration
		 *            progressbar duration
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder duration(int duration)
		{
			options.setDuration(duration);
			return this;
		}

		/**
		 * Sets the increment option value
		 * 
		 * @param increment
		 *            progressbar increment
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder increment(int increment)
		{
			options.setIncrement(increment);
			return this;
		}

		/**
		 * Sets the interval option value
		 * 
		 * @param interval
		 *            progressbar interval
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder interval(int interval)
		{
			options.setInterval(interval);
			return this;
		}

		/**
		 * Sets the width option value
		 * 
		 * @param width
		 *            progressbar width
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder width(int width)
		{
			options.setWidth(width);
			return this;
		}

		/**
		 * Sets the range option value
		 * 
		 * @param range
		 *            progressbar range
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder range(boolean range)
		{
			options.setRange(range);
			return this;
		}

		/**
		 * Sets the text option value
		 * 
		 * @param text
		 *            progressbar text
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder text(String text)
		{
			options.setText(text);
			return this;
		}

		/**
		 * Sets the textClass option value
		 * 
		 * @param textClass
		 *            progressbar textClass
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder textClass(String textClass)
		{
			options.setTextClass(textClass);
			return this;
		}

		/**
		 * Sets the start function
		 * 
		 * @param startScript
		 *            progressbar start function
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder startScript(String startScript)
		{
			options.setStartScript(startScript);
			return this;
		}

		/**
		 * Sets the pause function
		 * 
		 * @param pauseScript
		 *            progressbar pause function
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder pauseScript(String pauseScript)
		{
			options.setPauseScript(pauseScript);
			return this;
		}

		/**
		 * Sets the progress function
		 * 
		 * @param progressScript
		 *            progressbar progress function
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder progressScript(String progressScript)
		{
			options.setProgressScript(progressScript);
			return this;
		}

		/**
		 * Sets the stop function
		 * 
		 * @param stopScript
		 *            progressbar stop function
		 * @return JQProgressBarBuilder
		 */
		public JQProgressBarBuilder stopScript(String stopScript)
		{
			options.setStopScript(stopScript);
			return this;
		}

		/**
		 * @return JQProgressBar
		 */
		public JQProgressBar build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}